/* @vpotokov */

package com.example.tests;

import static org.junit.Assert.assertEquals;
import java.net.MalformedURLException;
import java.net.URL;
import org.junit.Before;
import org.junit.After;
import org.junit.Test;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public class RunEveryWhereTest {
	
    private WebDriver driver;
	
    @Before
    public void setUp() throws Exception {
        /* Choose the browser, version, and platform to test
        DesiredCapabilities capabilities = DesiredCapabilities.firefox();
        capabilities.setCapability("version", "5");
        capabilities.setCapability("platform", Platform.XP);

        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        capabilities.setCapability("version", "31");
        capabilities.setCapability("platform", "OS X 10.8");
        		
        DesiredCapabilities capabilities = DesiredCapabilities.firefox();
        capabilities.setCapability("version", "26");
        capabilities.setCapability("platform", "OS X 10.9");
		
        DesiredCapabilities capabilities = DesiredCapabilities.firefox();
        capabilities.setCapability("version", "27");
        capabilities.setCapability("platform", "Windows 8");

        DesiredCapabilities capabilities = DesiredCapabilities.iphone();
        capabilities.setCapability("version", "6.1");
        capabilities.setCapability("platform", "OS X 10.8");
        capabilities.setCapability("device-orientation", "portrait");
      		
        DesiredCapabilities capabilities = DesiredCapabilities.safari();
        capabilities.setCapability("version", "6");
        capabilities.setCapability("platform", "OS X 10.8");
        */
		
        DesiredCapabilities capabilities = DesiredCapabilities.firefox();
        capabilities.setCapability("version", "27");
        capabilities.setCapability("platform", "Windows XP");
                
        // Create the connection to Sauce Labs to run the tests
        // make sure you have a valid account 
        this.driver = new RemoteWebDriver(
            new URL("http://vpotokov:0e307857-xxxx-xxxx-xxxx-xxxxxxd4d8ea@ondemand.saucelabs.com:80/wd/hub"),
                capabilities);
    }

    @Test
    public void runInLocalBrowser() {
        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.google.ie");
        driver.quit();
    }
	
    /* The test requires php4dvd application be installed and running locally
     * also the selenium-standalone-server.jar is running. Otherwise it fails.
     */
    @Test
    public void runRemotelyOnLocalHost() {
        WebDriver driver = new RemoteWebDriver(
           DesiredCapabilities.firefox());
        try{
           driver.get("http://localhost/php4dvd");
           Thread.sleep(5000); // just observe 
        } catch (Exception e) {
           System.out.println("Page load has been skipped");
        }
        driver.quit();
    }

    /* The test expects selenium-standalone-server.jar is running on a remote system
     * (or equally on virtual system installed locally). It fails otherwise.
     */
    @Test
    public void runRemotelyOnRemoteHost() throws MalformedURLException {
        WebDriver driver = new RemoteWebDriver(
        new URL("http://192.168.1.29:4444/wd/hub"),
        DesiredCapabilities.firefox());
        driver.get("http://selenium2.ru");
		driver.get("http://www.amazon.com/");
        assertEquals("Amazon.com: Online Shopping for Electronics, Apparel, Computers, Books, DVDs & more", driver.getTitle());
        driver.get("http://www.rbc.ru/");
        try{
            driver.get("http://www.neckermann.be/general.aspx");
            Thread.sleep(5000);
        }
        catch (Exception e) {
            System.out.println("Page load has been skipped");
        }
        driver.quit();
		
        WebDriver driver1 = new RemoteWebDriver(
            new URL("http://192.168.1.29:4444/wd/hub"),
            DesiredCapabilities.firefox());
        driver1.get("http://www.google.ie");
        driver1.quit();
    }
	
    /* Just a wrapper that runs the below test twice */
    @Test
    public void runTest() throws Exception {
        int x = 0;
        while( x < 2 ) {
             System.out.println("value of x : " + x );
             webDriverOnCloud();
             x++;
        }
    }
	
	/* Should have your valid account for a cloud provider service that supports
	 * selenium Webdriver. It fails otherwise.
	 */
	public void webDriverOnCloud() throws Exception {
        // Make the browser get the page and check its title
        driver.get("http://www.amazon.com/");
        assertEquals("Amazon.com: Online Shopping for Electronics, Apparel, Computers, Books, DVDs & more", driver.getTitle());
        driver.get("http://www.google.com/");
        try{
            driver.get("http://www.neckermann.be/general.aspx");
            Thread.sleep(1000); // just a time to observe
            driver.get("http://www.online.ru");
            driver.get("http://www.yahoo.ie");
            Thread.sleep(1000);
        }
        catch (Exception e) {
            System.out.println("Page load has been skipped");
        }
    }
	
    @After
    public void tearDown() throws Exception {
        driver.quit();
    }
}
